package com.atguigu.gulimall.order.listener;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.atguigu.gulimall.order.config.AlipayTemplate;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.PayAsyncVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@RestController
public class OrderPayedListener {

       @Autowired
       AlipayTemplate alipayTemplate;

       @Autowired
       OrderService orderService;

       @PostMapping("/payed/notify")
       public String handleAlipayed(PayAsyncVo vo,HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
           //只要我们收到了支付宝给我的们的支付成功的异步通知，我们就会返回给支付宝一个success
           //支付宝就不会在通知了，不然支付宝会一直通知，最大8次
           //验证签名，是不是支付宝给我们发的数据
           //获取支付宝POST过来反馈信息
           Map<String,String> params = new HashMap<String,String>();
           Map<String,String[]> requestParams = request.getParameterMap();
           for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
               String name = (String) iter.next();
               String[] values = (String[]) requestParams.get(name);
               String valueStr = "";
               for (int i = 0; i < values.length; i++) {
                   valueStr = (i == values.length - 1) ? valueStr + values[i]
                           : valueStr + values[i] + ",";
               }
               //乱码解决，这段代码在出现乱码时使用
//               valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
               params.put(name, valueStr);
           }

           boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayTemplate.getAlipay_public_key(), alipayTemplate.getCharset(), alipayTemplate.getSign_type()); //调用SDK验证签名
           if(signVerified){
               //签名验证成功
               String result = orderService.handlePayResult(vo);
               return result;
           }else {
               return "error";
           }
       }
}
